package cloud.mbase.auth;

import cloud.mbase.business.interf.UserBusinessInterface;
import cloud.mbase.entity.datavo.Mrole;
import cloud.mbase.entity.datavo.Muser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MyUserDetailsServiceImpl implements UserDetailsService {
    @Autowired
    private UserBusinessInterface userBusiness;

    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        Muser user = userBusiness.findByUserName(s);
        List<Mrole> roles = user.getMroles();
        roles.forEach(r -> System.out.println(s + ":" + r.getAuthority()));
        return new User(user.getUserName(), user.getPassword(), roles);
    }
}
